<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DefaultController
 *
 * @author Zelic
 */
class ZF_DefaultController extends Zend_Controller_Action {

    public function init() {

        $module = $this->_request->getModuleName();
        $this->view->module = $module;
        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        $this->view->baseUrl = $baseUrl;
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $data = $auth->getStorage()->read();

            $this->view->profileNameSpace = (array) $data;
            $this->view->debug = (array) $data;
            if ($this->view->profileNameSpace['isAdmin'] == "0") {
                $this->view->isAdminLogin = "true";
            } else if ($this->view->profileNameSpace['isAdmin'] == "1") {
                $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
                if ($controller != "nhapdaily" && $controller != "donhang" && $controller != "index" && $controller != "dangnhap/logout") {
                    $this->_redirect("/index");
                }
                $this->view->isInputLogin = "true";
            }
            else {
                $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
                if ($controller != "tracuu" && $controller != "index" && $controller != "dangnhap/logout") {
                    $this->_redirect("/index");
                }
                $this->view->isGuestLogin = "true";
            }
        } else {
            $controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
            if ($controller != "index" && $controller != "dangnhap") {
                $this->_redirect("/index");
            }
        }
        $option = array("layout" => "index", "layoutPath" => APPLICATION_PATH . "/modules/default/layouts/scripts", "module" => "default");
        Zend_Layout::startMvc($option);
    }

    public function changeRouter($request, $map) {

        $configs = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routers.ini', 'config', true);
        $configs->routes->index->defaults = array(
            "module" => $request['module'],
            "controller" => $request['controller'],
            "action" => $request['action'],
        );

        $configs->routes->index->map = $map;

        $write = new Zend_Config_Writer_Ini();
        $write->write(APPLICATION_PATH . '/configs/routers.ini', $configs);
    }

}

?>
